package android.material.util;

import android.graphics.Point;
import android.graphics.Rect;

/**
 * 工具
 * <p/>
 * Created by Haidy on 15/1/1.
 */
public class Util {

    /**
     * 判断圆是否超出矩形的范围
     *
     * @param radius 圆的半径
     * @param point  圆心在矩形的位置
     * @param rect   矩形
     * @return 是否超出范围
     */
    public static boolean circleInRect(int radius, Point point, Rect rect) {
        int right = rect.width() - point.x;
        int bottom = rect.height() - point.y;
        return radius < Math.sqrt(Math.pow(point.x, 2) + Math.pow(point.y, 2)) || radius < Math.sqrt(Math.pow(point.x, 2) + Math.pow(bottom, 2)) || radius < Math.sqrt(Math.pow(right, 2) + Math.pow(point.y, 2)) || radius < Math.sqrt(Math.pow(bottom, 2) + Math.pow(right, 2));
    }

    /**
     * 点到矩形四个顶点的最远距离
     *
     * @param point 点
     * @param rect  矩形
     * @return 最远距离
     */
    public static double maxRangeToCorner(Point point, Rect rect) {
        int right = rect.width() - point.x;
        int bottom = rect.height() - point.y;
        double topLeft = Math.sqrt(Math.pow(point.x, 2) + Math.pow(point.y, 2));
        double topRight = Math.sqrt(Math.pow(right, 2) + Math.pow(point.y, 2));
        double bottomLeft = Math.sqrt(Math.pow(point.x, 2) + Math.pow(bottom, 2));
        double bottomRight = Math.sqrt(Math.pow(bottom, 2) + Math.pow(right, 2));
        return max(topLeft, topRight, bottomLeft, bottomRight);
    }

    /**
     * 取最大值
     *
     * @param doubles double类型的数组
     * @return double
     */
    public static double max(double... doubles) {
        double max = doubles[0];
        for (double d : doubles) {
            if (d > max)
                max = d;
        }
        return max;
    }

    /**
     * 判断点是否在rect中
     *
     * @param p    点
     * @param rect 范围
     * @return bool
     */
    public static boolean inRect(Point p, Rect rect) {
        return p.x > rect.left && p.x < rect.right && p.y > rect.top && p.y < rect.bottom;
    }
}
